//
//  GQHOrderDetailView.m
//  Xanthium
//
//  Created by GuanQinghao on 14/05/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHOrderDetailView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"
#import "YHPhotoBrowser.h"
#import "UIButton+YYWebImage.h"

@interface GQHOrderDetailView () <UITableViewDelegate,UITableViewDataSource> {
    
    NSArray *rowTitleArray;
}

@end


@implementation GQHOrderDetailView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    rowTitleArray = @[@"司机姓名",@"货物信息",@"预付款",@"尾款",@"其他费用",@"运费总计",@"装车时间",@"装货地点",@"装货联系人",@"送达时间",@"送达地点",@"收货联系人"];
    if (!_qh_contentArray) {
        
        _qh_contentArray = [NSMutableArray arrayWithArray:@[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""]];
    }
}

#pragma mark --View
- (void)loadUserInterface {
    
    UIView *orderNumberView = [[UIView alloc] init];
    orderNumberView.backgroundColor = kAppRedColor;
    [self addSubview:orderNumberView];
    [orderNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.height.mas_equalTo(30);
    }];
    
    _qh_orderNumberLabel = [[UILabel alloc] init];
    _qh_orderNumberLabel.font = kSmallerFont;
    _qh_orderNumberLabel.textColor = kWhiteColor;
    [orderNumberView addSubview:_qh_orderNumberLabel];
    [_qh_orderNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.mas_equalTo(orderNumberView);
        make.left.and.right.mas_equalTo(orderNumberView).with.inset(GQH_MARGIN_SINGLE);
    }];
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight + 30);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

- (void)setImageUrlArray:(NSArray *)imageUrlArray
{
    _imageUrlArray = imageUrlArray;
    
    self.qh_tableView.tableFooterView = [self footerView];
}

- (UIView *)footerView
{
    UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.qh_width, 60)];
    footerView.backgroundColor = [UIColor whiteColor];
    
    UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, 80, footerView.qh_height)];
    title.text = @"回执单";
    title.textColor = [UIColor darkTextColor];
    [footerView addSubview:title];
    
    for (int i = 0; i < self.imageUrlArray.count; i ++)
    {
        NSString * url = self.imageUrlArray[i];
        UIButton * imageView = [[UIButton alloc] initWithFrame:CGRectMake(self.qh_width - (50 + 5) * (i + 1) - 10, 5, 50, 50)];
        imageView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
        [imageView addTarget:self action:@selector(viewImage:) forControlEvents:UIControlEventTouchUpInside];
        [imageView yy_setImageWithURL:[NSURL URLWithString:url] forState:0 options:0];
        imageView.tag = i;
        [footerView addSubview:imageView];
    }
    
    return footerView;
}

- (void)viewImage:(UIButton *)sender
{
    CGRect rect1 = [sender convertRect:sender.frame fromView:sender.superview];//获取imagevuew在subview的位置
    CGRect rect2 = [sender convertRect:rect1 toView:self];
    
    YHPhotoBrowser *photoView = [[YHPhotoBrowser alloc]init];
    photoView.sourceView = self;//图片所在的父容器
    photoView.urlImgArr = self.imageUrlArray; //网络链接图片的数组
    photoView.sourceRect = rect2; // 图片的frame
    photoView.indexTag = sender.tag;   //初始化进去显示的图片下标
    [photoView show];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return rowTitleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    GQHCustomTextDoubleHorizontalCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHCustomTextDoubleHorizontalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.qh_leftLabel.text = rowTitleArray[indexPath.row];
    cell.qh_rightLabel.text = _qh_contentArray[indexPath.row];
    
    /// 重新布局
    [cell.qh_leftLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(cell.contentView.mas_centerX).with.inset(50);
    }];
    [cell.qh_rightLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(cell.contentView.mas_centerX).with.inset(-50);
    }];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

@end
